#!/bin/bash

echo "🧪 Verifying Model Table Mappings..."
echo ""

# Check each model file
models=(
    "app/common/model/Device.php:gf_device"
    "app/common/model/ChannelStats.php:gf_channel_stats"
    "app/common/model/AttributionEvent.php:gf_attribution_event"
    "app/common/model/AttributionInstall.php:gf_attribution_install"
    "app/common/model/AttributionTrack.php:gf_attribution_track"
)

success_count=0
total_count=${#models[@]}

for model in "${models[@]}"; do
    IFS=':' read -r file expected_table <<< "$model"
    
    model_name=$(basename "$file" .php)
    echo "Testing: $model_name"
    echo "Expected table: $expected_table"
    
    if [[ -f "$file" ]]; then
        # Use grep to find the table property
        table_line=$(grep -n "protected.*table.*=" "$file" | head -1)
        
        if [[ -n "$table_line" ]]; then
            # Extract the table name from the line
            actual_table=$(echo "$table_line" | sed -E "s/.*protected.*table.*=.*['\"]([^'\"]+)['\"].*/\1/")
            
            echo "  Actual table: $actual_table"
            
            if [[ "$actual_table" == "$expected_table" ]]; then
                echo "  ✅ Table name matches expected value"
                ((success_count++))
            else
                echo "  ❌ Table name mismatch: expected $expected_table, got $actual_table"
            fi
        else
            echo "  ❌ No table property found"
        fi
    else
        echo "  ❌ File not found: $file"
    fi
    
    echo ""
done

echo "📊 TEST RESULTS:"
echo "=================================================="
echo "Total models tested: $total_count"
echo "✅ Successful: $success_count"
echo "❌ Failed: $((total_count - success_count))"
echo "Success rate: $(echo "scale=2; $success_count * 100 / $total_count" | bc -l)%"

if [[ $success_count -eq $total_count ]]; then
    echo ""
    echo "🎉 ALL MODEL TABLE MAPPINGS ARE CORRECT!"
    echo "✅ All models use the expected gf_ prefixed table names."
else
    echo ""
    echo "⚠️  Some issues were found. Please review the errors above."
fi

echo ""
echo "🏁 Model table mapping verification completed!"

# Additional verification - check all model files
 echo ""
echo "🔍 ADDITIONAL VERIFICATION - All Model Files:"
echo "=================================================="

all_models=$(find app/common/model -name "*.php" | sort)
models_need_update=()

while IFS= read -r file; do
    if [[ -f "$file" ]]; then
        table_line=$(grep "protected.*table.*=" "$file" 2>/dev/null | head -1)
        if [[ -n "$table_line" ]]; then
            table_name=$(echo "$table_line" | sed -E "s/.*protected.*table.*=.*['\"]([^'\"]+)['\"].*/\1/")
            if [[ ! "$table_name" =~ ^gf_ ]]; then
                models_need_update+=("$(basename "$file" .php): $table_name")
            fi
        fi
    fi
done <<< "$all_models"

if [[ ${#models_need_update[@]} -gt 0 ]]; then
    echo "⚠️  Models that still need gf_ prefix update:"
    for model in "${models_need_update[@]}"; do
        echo "  - $model"
    done
else
    echo "✅ All models use gf_ prefixed table names!"
fi

echo ""